package com.fira.api.controller;

import java.util.List;

import com.fira.api.domain.dto.LoginDTO;
import com.fira.common.annotation.RepeatSubmit;
import com.fira.common.constant.Constants;
import com.fira.common.core.domain.platform.vo.PlatformTenantVO;
import com.fira.common.utils.DateUtils;
import com.fira.common.utils.StringUtils;
import com.fira.edu.domain.*;
import com.fira.edu.domain.vo.StudentParentsVO;
import com.fira.edu.service.*;
import com.fira.platform.service.IPlatformTenantService;
import com.fira.system.domain.dto.SysNoticeMessageDTO;
import com.fira.system.domain.vo.SysMessageListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fira.common.annotation.Log;
import com.fira.common.enums.BusinessType;
import com.fira.system.domain.SysNoticeMessage;
import com.fira.system.service.ISysNoticeMessageService;
import com.fira.common.core.controller.BaseController;
import com.fira.common.core.domain.AjaxResult;
import com.fira.common.utils.poi.ExcelUtil;
import com.fira.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 系统通知消息Controller
 * 
 * @author zhushuyong
 * @date 2021-10-26
 */
@Controller
@RequestMapping("/api/message")
@Api(value="系统通知消息接口",tags={"系统通知消息"})
public class SysNoticeMessageController extends ApiController
{
    @Autowired
    private ISysNoticeMessageService sysNoticeMessageService;
    @Autowired
    private IEduClassService eduClassService;
    @Autowired
    private IEduStudentCourseService eduStudentCourseService;
    @Autowired
    private IEduStudentService eduStudentService;
    @Autowired
    private IEduParentService eduParentService;
    @Autowired
    private IEduCourseService eduCourseService;
    @Autowired
    private IEduCourseScheduleTimeService eduCourseScheduleTimeService;
    @Autowired
    private IPlatformTenantService platformTenantService;

    @ApiOperation(value="查询系统通知分类消息数量",tags={"查询系统通知分类消息数量"},notes="")
    @PostMapping("/getNoticeTypeCount")
    @ResponseBody
    public AjaxResult getNoticeTypeCount(HttpServletRequest request){
        LoginDTO loginDTO = getLogin(request);
        String parentId = loginDTO.getParentId();
        if(StringUtils.isEmpty(parentId)){
            return AjaxResult.error("请先登录");
        }
        return AjaxResult.success("查询成功",sysNoticeMessageService.selectNoticeTypeCount(parentId));
    }

    @ApiOperation(value="查询系统通知消息列表(包含未读列表和已读列表)",tags={"查询系统通知消息列表(包含未读列表和已读列表)"},notes="")
    @PostMapping("/getMessageList")
    @ResponseBody
    public AjaxResult getMessageList(HttpServletRequest request){
        LoginDTO loginDTO = getLogin(request);
        String parentId = loginDTO.getParentId();
        if(StringUtils.isEmpty(parentId)){
            return AjaxResult.error("请先登录");
        }
        SysMessageListVO vo = new SysMessageListVO();
        SysNoticeMessage sysNoticeMessage = new SysNoticeMessage();
        sysNoticeMessage.setReceiverId(parentId);
        sysNoticeMessage.setNoticeType(request.getParameter("noticeType"));
        //未读列表
        sysNoticeMessage.setReadStatus(Constants.STATUS_ZERO);
        List<SysNoticeMessage> noReadMessageList = sysNoticeMessageService.selectSysNoticeMessageList(sysNoticeMessage);
        vo.setNoReadList(noReadMessageList);
        //已读列表
        sysNoticeMessage.setReadStatus(Constants.STATUS_ONE);
        List<SysNoticeMessage> readedMessageList = sysNoticeMessageService.selectSysNoticeMessageList(sysNoticeMessage);
        vo.setReadedList(readedMessageList);
        return AjaxResult.success("查询成功",vo);
    }

    /**
     * 查询系统通知消息列表
     */
    @ApiOperation(value="查询系统通知消息列表",tags={"查询系统通知消息列表"},notes="")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(HttpServletRequest request)
    {
        LoginDTO loginDTO = getLogin(request);
//        String tenantId = loginDTO.getCurrentTenantId();
        String parentId = loginDTO.getParentId();
        SysNoticeMessage sysNoticeMessage = new SysNoticeMessage();
        sysNoticeMessage.setReceiverId(parentId);
        startPage();
        List<SysNoticeMessage> list = sysNoticeMessageService.selectSysNoticeMessageList(sysNoticeMessage);
        return AjaxResult.success("查询成功",getDataTable(list));
    }

    /**
     * 新增保存系统通知消息
     */
    @ApiOperation(value="上课通知消息",tags={"上课通知消息"},notes="")
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(@Valid SysNoticeMessageDTO sysNoticeMessageDTO, HttpServletRequest request)
    {
        LoginDTO loginDTO = getLogin(request);
        String tenantId = loginDTO.getCurrentTenantId();
        //查询租户信息
        PlatformTenantVO platformTenantVO = platformTenantService.selectPlatformTenantById(tenantId);
        sysNoticeMessageDTO.setTenantId(tenantId);
        sysNoticeMessageDTO.setSenderId(loginDTO.getTeacherId());
        sysNoticeMessageDTO.setSchoolName(platformTenantVO.getTenantName());
        if(StringUtils.isEmpty(sysNoticeMessageDTO.getClassId())){
            if(sysNoticeMessageService.addMessage(sysNoticeMessageDTO) == 0){
                return AjaxResult.error("添加失败");
            }
        }else{
            EduClass eduClass = eduClassService.selectEduClassById(sysNoticeMessageDTO.getClassId());
            if(eduClass == null){
                return AjaxResult.error("班级不存在");
            }
            EduCourse eduCourse = eduCourseService.selectEduCourseById(eduClass.getCourseId());
            sysNoticeMessageDTO.setClassName(eduClass.getClassName());
            sysNoticeMessageDTO.setCourseName(eduCourse.getCourseName()+"-"+eduCourse.getCourseFlag());
            //查询班级下的学生
            EduStudentCourse eduStudentCourse = new EduStudentCourse();
            eduStudentCourse.setClassId(sysNoticeMessageDTO.getClassId());
            eduStudentCourse.setCourseId(eduClass.getCourseId());
            List<EduStudentCourse> studentCourseList = eduStudentCourseService.selectEduStudentCourseList(eduStudentCourse);
            if(CollectionUtils.isEmpty(studentCourseList)){
                return AjaxResult.error("班级没有学生");
            }
            //查询学生对应的家长
            studentCourseList.forEach(item -> {
                List<StudentParentsVO> parentVOList = eduStudentService.selectStudentParents(item.getStudentId());
                EduStudent eduStudent = eduStudentService.selectEduStudentById(item.getStudentId());
                sysNoticeMessageDTO.setStudentName(eduStudent.getStudentName());
                sysNoticeMessageDTO.setToclassDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,eduCourseScheduleTimeService.selectEduCourseScheduleTimeById(sysNoticeMessageDTO.getCourseScheduleTimeId()).getCourseDate()));
                parentVOList.forEach(parent -> {
                    EduParent eduParent = new EduParent();
                    eduParent.setParentPhone(parent.getParentPhone());
                    List<EduParent> eduParentList = eduParentService.selectEduParentList(eduParent);
                    sysNoticeMessageDTO.setReceiverId(eduParentList.get(0).getId());
                    sysNoticeMessageService.addMessage(sysNoticeMessageDTO);
                });
            });
        }

        return AjaxResult.success("添加成功");
    }

    /**
     * 修改保存系统通知消息
     */
    @ApiOperation(value="修改系统通知消息",tags={"修改系统通知消息"},notes="")
    @PostMapping("/edit")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(SysNoticeMessage sysNoticeMessage,HttpServletRequest request)
    {
        LoginDTO loginDTO = getLogin(request);
        String parentId = loginDTO.getParentId();
        if(StringUtils.isEmpty(parentId)){
            return AjaxResult.error("请先登录");
        }
        if(StringUtils.isEmpty(sysNoticeMessage.getId())){
            return AjaxResult.error("消息ID不能为空");
        }
        if(StringUtils.isEmpty(sysNoticeMessage.getReadStatus())){
            sysNoticeMessage.setReadStatus(Constants.STATUS_ONE);
        }
        return toAjax(sysNoticeMessageService.updateSysNoticeMessage(sysNoticeMessage));
    }

    /**
     * 删除系统通知消息
     */
    @ApiOperation(value="删除系统通知消息",tags={"删除系统通知消息"},notes="")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysNoticeMessageService.deleteSysNoticeMessageByIds(ids));
    }

    /**
     * 查询通知消息详情
     */
    @ApiOperation(value="查询通知消息详情",tags={"查询通知消息详情"},notes="")
    @PostMapping( "/info")
    @ResponseBody
    public AjaxResult info(String id){
        if(StringUtils.isEmpty(id)){
            return AjaxResult.error("消息ID不能为空");
        }
        return AjaxResult.success("查询成功",sysNoticeMessageService.selectSysNoticeMessageById(id));
    }
}
